import { ref } from "vue"
import service from "@/axios"

export default function useMatch() {
    let matches = ref({
        id: "",
        startDate: "",
        itemName: "",
        title: "",
        homeName: "",
        awayName: "",
        homeScore: "",
        awayscore: "",
        winner: "",
    });

    async function getAllMatches() {
        await service.get("/matches").then((res) => {
            matches.value = res.data.data.map(item => {
                const dateTimeParts = item.startDate.split('T'); // 分割日期和时间
                const datePart = dateTimeParts[0]; // 获取日期部分
                const timePart = dateTimeParts[1].substring(0, 5); // 获取时分部分

                // 组合新的 startDate
                item.startDate = `${datePart} ${timePart}`;
                return item;
            });
        }).catch((err) => {
            console.error(err);
        })
    }

    async function getMatchesByDate(date) {
        await service.get(`/matchByStartDate?startDate=${date}`).then((res) => {
            matches.value = res.data.data.map(item => {
                const dateTimeParts = item.startDate.split('T'); // 分割日期和时间
                const datePart = dateTimeParts[0]; // 获取日期部分
                const timePart = dateTimeParts[1].substring(0, 5); // 获取时分部分

                // 组合新的 startDate
                item.startDate = `${datePart} ${timePart}`; 
                return item;
            });
        }).catch((err) => {
            console.error(err);
        })
    }

    return {
        matches,
        getAllMatches,
        getMatchesByDate,
    }
}